﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sherwood.Configuration
{
    public class BaseConfigurationElement : System.Configuration.ConfigurationElement
    {
        private string _innerXml = "";

        protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
        {
            string xml = reader.ReadOuterXml();
            System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("^(<([a-zA-Z]+)\\b[^>]*>)(.*?)(</\\2>)$", System.Text.RegularExpressions.RegexOptions.Singleline);
            System.Text.RegularExpressions.Regex reXmlNs = new System.Text.RegularExpressions.Regex("xmlns=\"[^\"]+\"");
            System.Text.RegularExpressions.Match match = re.Match(xml);
            if (match.Success)
            {
                xml = reXmlNs.Replace((match.Groups[1].Value + match.Groups[4].Value), "");
                _innerXml = match.Groups[3].Value;
            }
            else
            {
                xml = reXmlNs.Replace(xml, "");
            }
            System.IO.StringReader sr = new System.IO.StringReader(xml);
            reader = new System.Xml.XmlTextReader(sr);
            reader.Read();//move past first element
            base.DeserializeElement(reader, serializeCollectionKey);
            sr.Close();
        }

        public string InnerXml
        {
            get
            {
                return _innerXml ?? "";
            }
        }
    }
}
